<template>
  <div  class="articlelist">
    <van-pull-refresh :success-text="issuccess" success-duration="1500" v-model="isLoading" @refresh="onRefresh">
      <van-list
      v-model="loading"
      :error.sync="iserror"
      error-text="加载出错了欸"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      >
      <article-item v-for="item in list" :key="item.art_id+''" :obj="item"></article-item>
</van-list>

    </van-pull-refresh>

  </div>
</template>

<script>
import { getArticlelist } from '@/api/article'
import ArticleItem from '../../../components/ArticleItem.vue'
export default {
  components: { ArticleItem },
  props: {
    channel_id: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: Date.now(),
      iserror: false,
      isLoading: false,
      issuccess: ''
    }
  },

  created () {

  },

  methods: {
    async onLoad () {
      try {
        const res = await getArticlelist({
          channel_id: this.channel_id,
          timestamp: this.timestamp,
          with_top: 1
        })
        if (Math.random() > 0.5) {
          console.lg(2)
        }
        this.list.push(...res.results)
        this.timestamp = res.pre_timestamp
        this.loading = false
        if (!res.results.length) {
          this.finished = true
        }
      } catch (error) {
        this.iserror = true
        this.loading = false
      }
      //  1.进入页面  并且显示list列表组件就会触发
      // 2.加载的数据没有占满一屏  继续加载下一页
      // 3触底的时候
      // 发送请求获取数据
      // 保存数据
      // this.list.push(this.list.length+1)
      // 3.让当前这一次的加载状态结束
      // this.loading = false
    // 4.数据全部加载完成
    // this.finished = true
    },
    async onRefresh () {
      try {
        const res = await getArticlelist({
          channel_id: this.channel_id,
          timestamp: Date.now(),
          with_top: 1
        })
        if (Math.random() > 0.5) {
          console.lg(2)
        }
        this.list.unshift(...res.results)
        this.isLoading = false
        this.issuccess = '刷新成功'
      } catch (error) {
        console.log(error)
        this.isLoading = false
        this.issuccess = '失败了哦'
      }
    }
  }
}
</script>

<style scoped lang='less'>
.articlelist{
  height: 80vh;
  overflow-y: auto;
}
</style>
